package com.example.matchandroiddevices.test.readers;

import java.util.List;

public class InventoryScanResult {
    /**
     * Inventory-scan命令的结果类
     */
        public boolean isSuccess;        // 是否成功
        public int responseLength;       // 响应长度
        public int readerAddress;        // 读卡器地址
        public int status;               // 状态码
        public String message;           // 成功消息
        public String errorMessage;      // 错误信息
        public List<TagData> tagUids;    // 标签UID列表

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append("Inventory-scan结果: ").append(isSuccess ? "成功" : "失败").append("\n");

            if (isSuccess) {
                sb.append(message).append("\n");

                if (tagUids != null && !tagUids.isEmpty()) {
                    sb.append("找到的标签数量: ").append(tagUids.size()).append("\n");

                    for (int i = 0; i < tagUids.size(); i++) {
                        TagData tag = tagUids.get(i);
                        sb.append("标签 ").append(i + 1).append(":\n");
                        sb.append("  DSFID: 0x").append(String.format("%02X", tag.dsfid & 0xFF)).append("\n");
                        sb.append("  UID: ").append(tag.getUid()).append("\n");
                    }
                }
            } else {
                sb.append("错误: ").append(errorMessage).append("\n");
            }

            sb.append("响应长度: ").append(responseLength).append("\n");
            sb.append("读卡器地址: 0x").append(String.format("%02X", readerAddress)).append("\n");
            sb.append("状态码: 0x").append(String.format("%02X", status)).append("\n");

            return sb.toString();
        }

    /**
     * 标签数据类
     */
    public static class TagData {
        public byte dsfid;    // 数据存储格式标识符
        public byte[] uid;    // 唯一标识符(8字节)

        @Override
        public String toString() {
            return String.format("DSFID: 0x%02X, UID: %s", dsfid & 0xFF, getUid());
        }
        public String getUid(){
            return byteArrayToHexString(this.uid);
        }
        private static String byteArrayToHexString(byte[] data)
        {
            StringBuilder sb = new StringBuilder(data.length * 3);
            for (byte b : data)
                sb.append(String.format("%-3s", String.format("%02x", b)));
            return sb.toString().toUpperCase();
        }
    }
}
